//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop


#include "Eic.h"
#include <Outils.h>
#include "MTAG_CtrlStd.h"
#include "CComboBx.h"
#pragma package(smart_init)

#define MODULE_TAG MTAG_CCOMBOBOX

//---------------------------------------------------------------------------
#define COL_EDIT_LISTBOX        clWindow
#define TOP_LIBELLE_COMBOBOX    3
//---------------------------------------------------------------------------
// ValidCtrCheck est utilisé est utilisé pour être sûr que les composants créés n'ont
// aucune fonction virtuelle pure.

static inline void ValidCtrCheck(CCustomComboBox *)
{
  new CCustomComboBox(NULL);
}
//---------------------------------------------------------------------------
__fastcall CCustomComboBox::CCustomComboBox(TComponent* Owner)
  : TCustomComboBox(Owner)
{
    m_asRefWebHelp              = EmptyStr;
    m_DroppedWidth = 0;
    FLibelleFont = 0;
    FReadOnly                   = false;
    inherited::Enabled          = true;
    inherited::Color            = COL_EDIT_LISTBOX;
    inherited::Visible          = true;
    inherited::TabStop          = true;
    inherited::Visible          = true;
    FObligatoire                = false;
    FBleuSiHint                 = false;

    FSizeEsp                    = ECART_LIBEL_COMBOBOX;
    m_pfnOnEstCorrectementRempli= NULL;

    try {
        FLibelleFont            = new TFont();
        FLibelleFont->Assign( Font );
        LLibelle                = new TLabel( this );
        LLibelle->Transparent   = true;
        Libelle                 = "";
        LLibelle->FocusControl  = this;
        FLibelleFont->OnChange  = LibelleFontChange;
        IsEcranChaine           = true;
        FTextCompletion         = false;
        RafraichirFond(this);
        }
#ifdef _DEBUG
    catch(Exception &e)
       {
       EICDisplayException(TAG(10), e);
       }
    catch(...)
       {
       ASSERT(false);
       }
#else
    catch(...)
       {
       }
#endif

}
/*******************************************************************************

Fonction :

Paramètres d'entrée :

Paramètres modifiés :

Valeur retournée :
*******************************************************************************/

void __fastcall CCustomComboBox::SetDroppedWidth(int Largeur)
   {
   ::SendMessage(this->Handle,CB_SETDROPPEDWIDTH,(bool)(m_DroppedWidth = Largeur) ? Largeur : Width,0);
   }

/*******************************************************************************

Fonction :

Paramètres d'entrée :

Paramètres modifiés :

Valeur retournée :
*******************************************************************************/
bool __fastcall CCustomComboBox::StoredDroppedWidth()
   {
   return m_DroppedWidth;
   }

/*******************************************************************************

Fonction :

Paramètres d'entrée :

Paramètres modifiés :

Valeur retournée :
*******************************************************************************/
void __fastcall CCustomComboBox::ReplaceLibelle()
   {
   LLibelle->Left = Left - (FSizeEsp >= 0 ? LLibelle->Width + FSizeEsp : -FSizeEsp);
   LLibelle->Top = Top - (LLibelle->Height - Height) / 2;
   }

//---------------------------------------------------------------------------
__fastcall CCustomComboBox::~CCustomComboBox()
{
  delete FLibelleFont;
  FLibelleFont = 0;
}
//---------------------------------------------------------------------------
void __fastcall CCustomComboBox::SetVisible(bool nValue)
{
  inherited::Visible = nValue;
  LLibelle ->Visible = nValue;
}
//---------------------------------------------------------------------------
bool __fastcall CCustomComboBox::GetVisible(void) const
{
  return inherited::Visible;
}
//---------------------------------------------------------------------------
void __fastcall CCustomComboBox::SetEnabled( bool bEnabled )
{
    inherited::Enabled      = bEnabled;
    if( LLibelle )
        LLibelle->Enabled   = bEnabled;

    if( bEnabled )
        SetReadOnly( FReadOnly );
    else
        RafraichirFond(this);
}
//---------------------------------------------------------------------------
void __fastcall CCustomComboBox::SetLibelle( const AnsiString aLibelle )
{
    FLibelle = aLibelle;

    if( LLibelle ) {
        LLibelle->Visible    = Visible;
        LLibelle->Enabled    = Enabled;
        LLibelle->Parent     = Parent;
        LLibelle->Font       = LibelleFont;
        LLibelle->Caption    = FLibelle;
        LLibelle->Alignment  = taRightJustify;
        ReplaceLibelle();
//        LLibelle->Top        = Top   + TOP_LIBELLE_COMBOBOX;
//        LLibelle->Left       = Left  - LLibelle->Width - FSizeEsp;
    }
}
//---------------------------------------------------------------------------
void __fastcall CCustomComboBox::SetLibelleFont( TFont *FFont )
{
    FLibelleFont->Assign( FFont );
}
//---------------------------------------------------------------------------
void __fastcall CCustomComboBox::SetReadOnly( bool bReadOnly )
{
    FReadOnly           = bReadOnly;
    inherited::TabStop  = !bReadOnly;
    if( Parent && bReadOnly && GetFocus()==Handle)
        ::SetFocus( Parent->Handle );

    RafraichirFond(this);
}
//---------------------------------------------------------------------------
void __fastcall CCustomComboBox::SetSizeEsp( int iSizeEsp )
{
    FSizeEsp = iSizeEsp;

    if( LLibelle )
      ReplaceLibelle();
//        LLibelle->Left = Left - LLibelle->Width - FSizeEsp;
}
//---------------------------------------------------------------------------
bool __fastcall CCustomComboBox::GetEnabled()
{
    return( inherited::Enabled );
}
//---------------------------------------------------------------------------
TFont * __fastcall CCustomComboBox::GetLibelleFont() const
{
    return( FLibelleFont );
}
//---------------------------------------------------------------------------
void __fastcall CCustomComboBox::LibelleFontChange( TObject *Sender )
{
    if( LLibelle ) {
        LLibelle->Font->Assign( FLibelleFont );
        ReplaceLibelle();
//        LLibelle->Top = Top - ( LLibelle->Height - Height ) / 2;
    }
}
//---------------------------------------------------------------------------
void __fastcall CCustomComboBox::KeyDown( Word &Key, Classes::TShiftState Shift)
{
   Classes::TShiftState ShiftTemp;

   ShiftTemp = ShiftTemp << ssCtrl;

   if( IsEcranChaine ) {
      switch( Key ) {
         case VK_NEXT:
            if( Shift == ShiftTemp )
               ItemIndex = (ItemIndex + DropDownCount - 1 < Items->Count )? (ItemIndex + DropDownCount - 1) : (Items->Count - 1);
            Key = VK_CANCEL;
            break;
         case VK_PRIOR:
            if( Shift == ShiftTemp )
               ItemIndex = (ItemIndex - DropDownCount + 1 >= 0 )? (ItemIndex - DropDownCount + 1) : 0;
            Key = VK_CANCEL;
            break;
      }
   }
    //WebHelp
    //-------
    if( (Key == VK_F3) && (Shift.Contains( ssCtrl )) ) {
          AccesWebHelp( m_asRefWebHelp );
    }else {
        if( Key == VK_F3 )
          VerifierRefWebHelp( m_asRefWebHelp );
    }
}
//---------------------------------------------------------------------------
void __fastcall CCustomComboBox::WndProc( TMessage &Message )
{
   switch( Message.Msg ) {
      case WM_SETFOCUS:
         if( !ComponentState.Contains( csDesigning )) {
            if( FReadOnly ) {
               Message.Msg = WM_NULL;
               ::SetFocus( Parent->Handle );
            }
         }
         break;
      case WM_ENABLE:
            Enabled = (bool)Message.WParam;
            break;
      case WM_READONLY:
            ReadOnly = (bool)Message.WParam;
            break;
      case WM_WINDOWPOSCHANGED:
            if( LLibelle ) {
                inherited::WndProc( Message );
                ReplaceLibelle();
//                LLibelle->Left  = Left  - LLibelle->Width    - FSizeEsp;
//                LLibelle->Top   = Top   - ( LLibelle->Height - Height ) / 2;
                Message.Msg = WM_NULL;
            }
            break;
      case WM_LBUTTONDBLCLK:
            inherited::DblClick();
      case WM_LBUTTONDOWN:
      case CB_SHOWDROPDOWN :
            if( !ComponentState.Contains( csDesigning )) {
                if( FReadOnly ) {
                    Message.Msg = WM_NULL;
                    ::SetFocus( Parent->Handle );
                }
             ::SendMessage(this->Handle,CB_SETDROPPEDWIDTH,m_DroppedWidth ,0);
            }
            break;
    }
    inherited::WndProc( Message );
}
//---------------------------------------------------------------------------
void __fastcall CCustomComboBox::ComboWndProc(Messages::TMessage &Message, HWND ComboWnd, void * ComboProc)
{
    switch( Message.Msg ) {
        case WM_KEYUP:
            if ( (Message.WParam != VK_BACK)
              && (Message.WParam != VK_DELETE)
              && (Message.WParam != VK_SHIFT)
              && (FTextCompletion) && (Style == Stdctrls::csDropDown)) {
                int rc = Perform(CB_FINDSTRING, -1,(int)Caption.c_str());
                if(rc != CB_ERR) {
                    int len = Text.Length();
                    ItemIndex = rc;
                    SelStart = len;
                    SelLength = Text.Length() - len;
                    Message.Result = 0;
                    Change();
                }
            }
            break;

        case WM_LBUTTONDBLCLK:
        case WM_LBUTTONDOWN:
        case WM_RBUTTONDOWN:
        case CB_SHOWDROPDOWN :
            if( !ComponentState.Contains( csDesigning )) {
                if( FReadOnly ) {
                    Message.Msg = WM_NULL;
                    ::SetFocus( Parent->Handle );
                }
            }
            break;
    }
    inherited::ComboWndProc( Message, ComboWnd, ComboProc );
}
//---------------------------------------------------------------------------
int __fastcall CCustomComboBox::GetItemIndex()
{
    return inherited::ItemIndex;
}
//---------------------------------------------------------------------------
void __fastcall CCustomComboBox::SetItemIndex(int iItemIndex)
{
    inherited::ItemIndex = iItemIndex;
    RafraichirFond(this);
}
//---------------------------------------------------------------------------
void __fastcall CCustomComboBox::Change( void)
{
    inherited::Change();
    RafraichirFond(this);
}
//---------------------------------------------------------------------------
bool __fastcall CCustomComboBox::EstCorrectementRempliDefault( void)
{
    return( ItemIndex != -1);
}
//---------------------------------------------------------------------------
void __fastcall CCustomComboBox::SetObligatoire( bool bObligatoire)
{
    FObligatoire = bObligatoire;
    RafraichirFond(this);
}
//---------------------------------------------------------------------------
void __fastcall CCustomComboBox::SetBleuSiHint( bool bBleuSiHint)
{
    FBleuSiHint = bBleuSiHint;
    RafraichirFond(this);
}

void __fastcall CCustomComboBox::SetText(const AnsiString aText)
{
inherited::Text = aText;
RafraichirFond(this);
}

AnsiString _fastcall CCustomComboBox::GetText()
{
return inherited::Text;
}
//---------------------------------------------------------------------------
__fastcall CComboBox::CComboBox(TComponent* Owner)
  : CCustomComboBox(Owner)
{

}
//---------------------------------------------------------------------------
bool __fastcall CComboBox::EstCorrectementRempliDefault( void)
{
    return( !Text.IsEmpty());
}
//---------------------------------------------------------------------------
namespace Ccombobx
{
  void __fastcall PACKAGE Register()
  {
     TComponentClass classes[1] = {__classid(CComboBox)};
     RegisterComponents("EIC Ctrlstd", classes, 0);
  }
}
//---------------------------------------------------------------------------

